package gravipong;


import java.util.ArrayList;

class ball extends paintable{
	
	ArrayList<paddle> pad;
    double speedX;
    double speedY;
    int gC = 40;
    double x2;
    double y2;
    double avgy;
    double avgx;
  
    public void move(ArrayList<gravitron> gravi){
    	int n = 0;
        if (y <= 0){speedY = (speedY-speedY*2);}
        else if (y >= 500){speedY = (speedY-speedY*2);}
        if (x <= 0){Main.changeScore(false);speedX = (speedX-speedX*2);}
        else if (x >= 500){Main.changeScore(true);speedX = (speedX-speedX*2);}
        if(x<=30)
        if(y<=pad.get(1).y+50)
        if(y>=pad.get(1).y)
        if(speedX<=0)speedX=speedX-(speedX*2);
        if(x>=470)
        if(y<=pad.get(0).y+50)
        if(y>=pad.get(0).y)
        if(speedX>=0)speedX=speedX-(speedX*2);
        for(gravitron g : gravi){
            double x3=x+(speedX/7.2);
            double y3=y+(speedY/7.2);
            while(n<9){
                if(n==0){x2 = x3-1;y2=y3+1;}
                if(n==1){x2 = x3; y2=y3+1;}
                if(n==2){x2=x3+1;y2=y3+1;}
                if(n==3){x2=x3-1;y2=y3;}
                if(n==4){x2=x3;y2=y3;}
                if(n==5){x2=x3+1;y2=y3;}
                if(n==6){x2=x3-1;y2=y3-1;}
                if(n==7){x2=x3;y2=y3-1;}
                if(n==8){x2=x3+1;y2=y3-1;}
                n++;
            if(x2 != g.x || y2 != g.y){
                double xdis = x2-g.x;
                double ydis = y2-g.y;
                if((xdis*xdis)<1)if(xdis<0)xdis=xdis-1;else xdis=xdis+1;
                if((ydis*ydis)<1)if(ydis<0)ydis=ydis-1;else ydis=ydis+1;
                double dis = (xdis*xdis)+(ydis*ydis);
                xdis = xdis*gC;
                ydis = ydis*gC;
                xdis = xdis/dis;
                ydis = ydis/dis;
                avgx = avgx-xdis;
                avgy = avgy-ydis;
            }}
            avgx = avgx/9;
            avgy = avgy/9;
            speedX = speedX+avgx;
            speedY = speedY+avgy;
            
        }
        x=x+speedX;
        y=y+speedY;
    }
    
    public ball(int a,int b, int c, int d,ArrayList<paddle> pads){
        super();
        pad = pads;
        speedX = a;
        speedY = b;
        x=c;
        y=d;
    }
}